local mod = {}
mod.name = minetest.get_current_modname()
mod.storage = minetest.get_mod_storage()


local function pos_to_string(pos)
	return string.format("%s %s %s", pos.x, pos.y, pos.z)
end

mod.start_mode = mod.storage:get_string("mode") or "NW"

minetest.register_chatcommand('plot', {
	params = '<+/-x> | <+/-y>| <+/-z> | set_name | set_counter | undo |[protect]',
	description = 'Automatically protect a preset area.',
	func = function(param)
		local privs = minetest.get_privilege_list()
		if not privs.teleport or not privs.fly or not privs.areas then
			minetest.display_chat_message("You don't have sufficient privs for this. Requries TELEPORT, FLY and AREAS")
			return
		end
		
		if not param or param == "" then --do the thing
			local pos = minetest.localplayer:get_pos()
			
			local area_size = {}
			area_size.px = mod.storage:get_int("+x")
			area_size.py = mod.storage:get_int("+y")
			area_size.pz = mod.storage:get_int("+z")
			area_size.mx = mod.storage:get_int("-x")
			area_size.my = mod.storage:get_int("-y")
			area_size.mz = mod.storage:get_int("-z")
			
			local positions = {
				{['x'] = pos.x - area_size.mx, ['y'] = pos.y - area_size.my, ['z'] = pos.z - area_size.mz},
				{['x'] = pos.x + area_size.px, ['y'] = pos.y + area_size.py, ['z'] = pos.z + area_size.pz}
			}
			local area_name = mod.storage:get_string("area_name") == "" and "Unnamed Plot" or mod.storage:get_string("area_name")
			local counter = mod.storage:get_int("counter")
			local f_name = string.format("%s %s", area_name, counter)
			minetest.display_chat_message(string.format("Beginning protection of %s", f_name))
			minetest.after(0,minetest.run_server_chatcommand,'teleport', pos_to_string(positions[1]))
			minetest.after(1,minetest.run_server_chatcommand,'area_pos1', "")
			minetest.after(1.5,minetest.run_server_chatcommand,'teleport',pos_to_string(positions[2]))
			minetest.after(2.5,minetest.run_server_chatcommand,'area_pos2', "")
			minetest.after(3,minetest.run_server_chatcommand,'teleport',pos_to_string(pos))
			minetest.after(3.5,minetest.run_server_chatcommand,'protect',f_name)
			minetest.after(4,minetest.display_chat_message, "Done")
			mod.storage:set_int("counter",counter+1)
			return
		else
			local param_s = param
			param = param:split(" ")
			
			for _,offset in pairs({"+x","+y","+z","-x","-y","-z"}) do
				if param[1] == offset then
					local amount = param[2]
					if not tonumber(param[2]) then
						minetest.display_chat_message("ERROR: Param[2] not a number")
						return
					end
					mod.storage:set_int(offset,amount)
					minetest.display_chat_message("Offset " .. offset .. " set to " .. amount)
					return
				end
			end
			
			if param[1] == "set_name" then
				local area_name = param_s:sub(10)
				minetest.display_chat_message("Areas will be prepended with '"..area_name.."'")
				mod.storage:set_string("area_name",area_name)
				return
			elseif param[1] == "set_counter" then
				if not tonumber(param[2]) then
					minetest.display_chat_message("ERROR: Param[2] not a number")
					return
				end
				mod.storage:set_int("counter",param[2])
				minetest.display_chat_message("Area counter updated to: "..param[2])
				return
			elseif param[1] == "undo" then
				local plot_num = mod.storage:get_int("counter") -1
				minetest.run_server_chatcommand('remove_area',tostring(plot_num))
				mod.storage:set_int("counter",plot_num)
				local area_name = mod.storage:get_string("area_name")
				minetest.display_chat_message(string.format("Removed last plot created: %s %s", area_name, plot_num))
				return
			end
		end
	end,
})
